package com.atguigu.exercise2;

/**
 *      1.编写Order类，有int型的orderId，String型的orderName，相应的
 *      getter()和setter()方法，两个参数的构造器，重写父类的equals()方法：
 *      public boolean equals(Object obj)，并判断测试类中创建的两个对象是否
 *      相等。
 *
 *
 * @author : leimingchao
 * @date Date : 2021年08月27日 21:16
 */
public class OrderTest {

    public static void main(String[] args) {
        Order order1 = new Order(1001,"AA");
        Order order2 = new Order(1001,"BB");
        System.out.println(order1.equals(order2));
        Order order3 = new Order(1001,"BB");
        System.out.println(order2.equals(order3));

        // String s1 = "BB";
        // String s2 = "BB";
        // //true
        // System.out.println(s1 == s2);
    }
}

class Order {
    private int orderId;
    private String orderName;

    public Order(int orderId, String orderName) {
        this.orderId = orderId;
        this.orderName = orderName;
    }

    public int getOrderId() {
        return orderId;
    }

    public void setOrderId(int orderId) {
        this.orderId = orderId;
    }

    public String getOrderName() {
        return orderName;
    }

    public void setOrderName(String orderName) {
        this.orderName = orderName;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Order order = (Order) o;

        if (orderId != order.orderId) return false;
        return orderName != null ? orderName.equals(order.orderName) : order.orderName == null;
    }

    @Override
    public int hashCode() {
        int result = orderId;
        result = 31 * result + (orderName != null ? orderName.hashCode() : 0);
        return result;
    }
}
